<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../lib/layui-v2.6.3/css/layui.css" media="all">
    <link rel="stylesheet" href="../css/public.css" media="all">
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">

        <fieldset class="table-search-fieldset">
            <legend>搜索信息</legend>
            <div style="margin: 10px 10px 10px 10px">
                <form class="layui-form layui-form-pane" action="">
                    <div class="layui-form-item">

                        <div class="layui-inline">
                            <label class="layui-form-label">k8s</label>
                            <div class="layui-input-inline">
                                <select name="k8sId" id="k8sId">
                                </select>
                            </div>

                        </div>
                        <div class="layui-inline">
                            <button type="submit" class="layui-btn layui-btn-primary layui-btn-sm" lay-submit
                                    lay-filter="data-search-btn"><i class="layui-icon"></i> 搜 索
                            </button>
                        </div>
                    </div>
                </form>
            </div>
        </fieldset>

        <script type="text/html" id="toolbarDemo">
            <div class="layui-btn-container">
                <button class="layui-btn layui-btn-normal layui-btn-sm data-add-btn" lay-event="add"> 通过JSON添加</button>
                <button class="layui-btn layui-btn-normal layui-btn-sm data-add-btn" lay-event="addByForm"> 通过表单添加</button>
<!--                <button class="layui-btn layui-btn-sm layui-btn-danger data-delete-btn" lay-event="delete"> 删除 </button>-->
            </div>
        </script>

        <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>

        <script type="text/html" id="currentTableBar">
            <a class="layui-btn layui-btn-primary layui-btn-xs data-count-edit" lay-event="detail">查看</a>
            <!--            <a class="layui-btn layui-btn-primary layui-btn-xs data-count-edit" lay-event="view">上线</a>-->
            <a class="layui-btn layui-btn-primary layui-btn-xs data-count-edit" lay-event="offline">下线</a>
            <a class="layui-btn layui-btn-primary layui-btn-xs data-count-edit" lay-event="restart">重启</a>
            <!--            <a class="layui-btn layui-btn-primary layui-btn-xs data-count-edit" lay-event="edit">发布</a>-->
            <a class="layui-btn layui-btn-normal layui-btn-xs data-count-edit" lay-event="edit">编辑</a>
            <!--            <a class="layui-btn layui-btn-xs layui-btn-danger data-count-delete" lay-event="delete">删除</a>-->
        </script>

    </div>
</div>
<script src="../lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
<script>
    layui.use(['form', 'table'], function () {
        var $ = layui.jquery,
            form = layui.form,
            table = layui.table;


        $.ajax({
            url: '../k8s/record/list',
            type: 'POST',
            data: {},
            async: false,
            success: function (result) {

                if (result.code === 10000) {
                    $('#k8sId').empty()
                    const selectList = result.data || []
                    selectList.forEach((selectItem, selectIndex) => {
                        $('#k8sId').append(`<option value="${selectItem.id}" ${selectIndex === 0?'selected':''} >${selectItem.clusterName}</option>`)
                    })
                    // $('#k8sId').append(`<option value="1"  >123</option>`)
                    form.render()

                    console.log( $('#k8sId').val())

                } else {
                    parent.layer.alert(result.msg)
                }

            }
        })

        var tableResult = table.render({
            elem: '#currentTableId',
            url: '../deploy/list' ,
            toolbar: '#toolbarDemo',
            where:{
                k8sId: $('#k8sId').val(),
                version: '123123'
            },
            defaultToolbar: ['filter', 'exports', 'print', {
                title: '提示',
                layEvent: 'LAYTABLE_TIPS',
                icon: 'layui-icon-tips'
            }],
            cols: [[
                // {type: "checkbox", width: 50},
                // {field: 'uid', width: 80, title: 'ID', sort: false},
                {field: 'name', title: '名称'},
                {field: 'namespace', width: 200, title: '命名空间'},
                {field: 'image', width: 280, title: '镜像'},
                {
                    width: 80, title: 'pods', align: 'center', templet: function (data) {
                        return `${data.readyReplicas}/${data.replicas}`
                    }
                },

                // {field: 'creationTimestamp', width: 270, title: 'creationTimestamp', sort: false},
                {field: 'createTime', width: 180, title: '创建时间', sort: false, align: 'center'},
                {
                    title: '操作', minWidth: 150, align: "center", templet: (data) => {

                        if (data.replicas === 0) {
                            let html = `
                                <a class="layui-btn layui-btn-primary layui-btn-xs data-count-edit" lay-event="detail">查看</a>
            <a class="layui-btn layui-btn-primary layui-btn-xs data-count-edit" lay-event="online">上线</a>
<!--            <a class="layui-btn layui-btn-primary layui-btn-xs data-count-edit" lay-event="offline">下线</a>-->
            <a class="layui-btn layui-btn-primary layui-btn-xs data-count-edit" lay-event="restart">重启</a>
            <a class="layui-btn layui-btn-normal layui-btn-xs data-count-edit" lay-event="edit">编辑</a>
                    `
                            return html
                        } else {
                            let html = `
                                <a class="layui-btn layui-btn-primary layui-btn-xs data-count-edit" lay-event="detail">查看</a>
<!--            <a class="layui-btn layui-btn-primary layui-btn-xs data-count-edit" lay-event="view">上线</a>-->
            <a class="layui-btn layui-btn-primary layui-btn-xs data-count-edit" lay-event="offline">下线</a>
            <a class="layui-btn layui-btn-primary layui-btn-xs data-count-edit" lay-event="restart">重启</a>
            <a class="layui-btn layui-btn-normal layui-btn-xs data-count-edit" lay-event="edit">编辑</a>
                    `
                            return html
                        }


                    }
                }
            ]],
            // limits: [10, 15, 20, 25, 50, 100],
            // limit: 15,
            page: false,
            limit: Number.MAX_VALUE,
            skin: 'row',
            parseData: function (res) { //res 即为原始返回的数据
                return {
                    "code": res.code === 10000 ? 0 : 500, //解析接口状态
                    "msg": res.code === 10000 ? '查询成功' : res.msg, //解析提示文本
                    "count": res.data?.length || 0, //解析数据长度
                    "data": res.data //解析数据列表
                };
            }
        });

        window.tableResult = tableResult

        // 监听搜索操作
        form.on('submit(data-search-btn)', function (data) {
            console.log( $('#k8sId').val())
            tableResult.reload({
                where:{
                    k8sId: $('#k8sId').val()
                }
            })
            return false;
        });

        /**
         * toolbar监听事件
         */
        table.on('toolbar(currentTableFilter)', function (obj) {
            if (obj.event === 'add') {  // 监听添加操作
                window.k8sId=$('#k8sId').val()
                var index = layer.open({
                    title: '添加Deployment',
                    type: 2,
                    shade: 0.2,
                    maxmin: true,
                    shadeClose: true,
                    area: ['100%', '100%'],
                    content: '../page/deployment-add.html',
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
            } else if (obj.event === 'addByForm') {  // 监听添加操作
                window.k8sId=$('#k8sId').val()
                var index = layer.open({
                    title: '通过表单添加Deployment',
                    type: 2,
                    shade: 0.2,
                    maxmin: true,
                    shadeClose: true,
                    area: ['800px', '700px'],
                    content: '../page/deployment-add-form.html',
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
            }  else if (obj.event === 'delete') {  // 监听删除操作
                var checkStatus = table.checkStatus('currentTableId')
                    , data = checkStatus.data;
                layer.alert(JSON.stringify(data));
            }
        });

        //监听表格复选框选择
        table.on('checkbox(currentTableFilter)', function (obj) {
            console.log(obj)
        });

        table.on('tool(currentTableFilter)', function (obj) {
            var data = obj.data;
            if (obj.event === 'delete') {
                // layer.confirm('真的删除行么', function (index) {
                //     obj.del();
                //     layer.close(index);
                // });
            } else if (obj.event === 'detail') {
                // 查看
                localStorage.setItem("currentNamespace", obj.data.namespace)
                localStorage.setItem("currentName", obj.data.name)
                window.k8sId=$('#k8sId').val()
                var index = layer.open({
                    title: '应用详情',
                    type: 2,
                    shade: 0.2,
                    maxmin: true,
                    shadeClose: true,
                    area: ['100%', '100%'],
                    content: '../page/deployment-detail.html',
                });
            } else if (obj.event === 'restart') {

                layer.confirm('确定重启?', {icon: 3, title: '提示'}, function (index) {
                    $.ajax({
                        url: `../deploy/restart/${obj.data.namespace}/${obj.data.name}` + '?k8sId=' +  $('#k8sId').val(),
                        success: function (result) {
                            if (result.code === 10000) {
                                layer.msg('命令发送成功')
                                tableResult.reload()
                            } else {
                                layer.msg(result.msg)
                            }

                        }
                    })

                    layer.close(index);
                });
            } else if (obj.event === 'offline') {

                layer.confirm('确定下线, 将replcaset设置成0 ?', {icon: 3, title: '提示'}, function (index) {
                    $.ajax({
                        url: `../deploy/offline/${obj.data.namespace}/${obj.data.name}` + '?k8sId=' +  $('#k8sId').val(),
                        success: function (result) {
                            if (result.code === 10000) {
                                layer.msg('发送成功')
                                tableResult.reload()
                            } else {
                                layer.msg(result.msg)
                            }

                        }
                    })

                    layer.close(index);
                });
            } else if (obj.event === 'online') {

                layer.confirm('确定上线, 将replcaset设置成1?', {icon: 3, title: '提示'}, function (index) {
                    $.ajax({
                        url: `../deploy/online/${obj.data.namespace}/${obj.data.name}` + '?k8sId=' +  $('#k8sId').val(),
                        success: function (result) {
                            if (result.code === 10000) {
                                layer.msg('发送成功')
                                tableResult.reload()
                            } else {
                                layer.msg(result.msg)
                            }

                        }
                    })

                    layer.close(index);
                });
            } else if (obj.event === 'edit') {
                // 查看
                localStorage.setItem("currentNamespace", obj.data.namespace)
                localStorage.setItem("currentName", obj.data.name)
                window.k8sId=$('#k8sId').val()
                var index = layer.open({
                    title: '应用详情',
                    type: 2,
                    shade: 0.2,
                    maxmin: true,
                    shadeClose: true,
                    area: ['80%', '80%'],
                    content: '../page/deployment-edit.html',
                });
            }
        });

    });
</script>

</body>
</html>
